package com.huizhonglm.diap.widget;

import android.content.Context;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.huizhonglm.diap.R;

/**
 * 三态界面（正在加载、加载失败，空数据）
 *
 * @author LiXiaoLong
 * @version 1.0
 * @since 2014-10-22 13:23:55
 */
public class LoadView extends RelativeLayout {
    /**
     * 状态-进度条
     */
    public static final int STATE_PROGRESS = 100;
    /**
     * 状态-页面加载失败
     */
    public static final int STATE_LOADERROR = 200;
    /**
     * 状态-正常页面
     */
    public static final int STATE_NORMAL = 300;
    /**
     * 状态-无数据
     */
    public static final int STATE_NO_DATA = 400;
    /**
     * 页面加载状态
     */
    private int pageState = 0;
    /**
     * 当前页面
     */
    private View contentView;
    /**
     * 进度条页面
     */
    private View progress;
    /**
     * 加载失败
     */
    private View error;
    /**
     * 无数据
     */
    private View empty;
    /**
     * 数据页面
     */
    private View dataView;
    /**
     * 无数据提示信息
     */
    private TextView emptyText;

    private OnLoadAgainListener mListener;

    public LoadView(Context context) {
        super(context);
        init(context);
    }

    public LoadView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        if (isInEditMode()) {
            return;
        }
        contentView = View.inflate(context, R.layout.widget_loadview, this);
        progress = contentView.findViewById(R.id.progressBar_lay);
        error = contentView.findViewById(R.id.loadError_msg_lay);
        empty = contentView.findViewById(R.id.noData_msg_lay);
        emptyText = (TextView) contentView.findViewById(R.id.noData_msg_tv);
        contentView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if (pageState == STATE_LOADERROR) {
                    if (mListener != null) {
                        showProgress();
                        mListener.onLoadAgain();
                    }
                }
            }
        });
        setBackground(getContext().getResources().getColor(R.color.background));
        showProgress();
    }

    public void setDataView(View dataView, OnLoadAgainListener listener) {
        this.dataView = dataView;
        this.mListener = listener;
    }

    private void setBackground(int resId) {
        contentView.setBackgroundColor(resId);
        progress.setBackgroundColor(resId);
        error.setBackgroundColor(resId);
        empty.setBackgroundColor(resId);
    }

    /**
     * 显示正常页面
     */
    public void showDataView() {
        pageState = STATE_NORMAL;
        contentView.setVisibility(View.GONE);
        progress.setVisibility(View.VISIBLE);
        error.setVisibility(View.GONE);
        empty.setVisibility(View.GONE);
        if (dataView != null) {
            dataView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 显示加载失败页面
     */
    public void showErrorView() {
        pageState = STATE_LOADERROR;
        contentView.setVisibility(View.VISIBLE);
        progress.setVisibility(View.GONE);
        error.setVisibility(View.VISIBLE);
        empty.setVisibility(View.GONE);
        if (dataView != null) {
            dataView.setVisibility(View.GONE);
        }
    }

    /**
     * 显示无数据页面
     */
    public void showEmptyView(String msg) {
        pageState = STATE_NO_DATA;
        if (!TextUtils.isEmpty(msg)) {
            emptyText.setText(msg);
        }
        contentView.setVisibility(View.VISIBLE);
        progress.setVisibility(View.GONE);
        error.setVisibility(View.GONE);
        empty.setVisibility(View.VISIBLE);
        if (dataView != null) {
            dataView.setVisibility(View.GONE);
        }
    }

    /**
     * 显示加载进度条页面
     */
    public void showProgress() {
        pageState = STATE_PROGRESS;
        progress.setVisibility(View.VISIBLE);
        error.setVisibility(View.GONE);
        empty.setVisibility(View.GONE);
        contentView.setVisibility(View.VISIBLE);
        // dataView.setVisibility(View.GONE);
    }


    public int getPageState() {
        return pageState;
    }

    public interface OnLoadAgainListener {
        void onLoadAgain();
    }
}
